package com.lemon.smartsafety.view.pop;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.view.list.MyListView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 项目名称：SmartSafeCode
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/12/22 14:35
 * 修改人：Fargo
 * 修改时间：2016/12/22 14:35
 * 修改备注：
 */
public class PopList {


    @Bind(R.id.listView)
    MyListView listView;
    @Bind(R.id.lineAll)
    LinearLayout lineAll;
    private PopupWindow popupWindow;
    private BackInterface dissMissBack;

    public void setDissMissBack(BackInterface dissMissBack) {
        this.dissMissBack = dissMissBack;
    }

    public PopList() {
    }

    public interface BackInterface {
        public void callBackFunction();

    }


    public void showPop(final Activity activity, View view, BaseAdapter adapter) {

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(activity).inflate(
                R.layout.pop_new_list, null);

        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


        popupWindow.setBackgroundDrawable(new BitmapDrawable());
     /*   ColorDrawable dw = new ColorDrawable(ContextCompat.getColor(activity,R.color.gray));
        popupWindow.setBackgroundDrawable(dw);
        backgroundAlpha(activity,0.9f);*/


        ButterKnife.bind(this, contentView);
        listView.setAdapter(adapter);


        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (dissMissBack != null) {
                    dissMissBack.callBackFunction();
                }

               /* Log.e("List_noteTypeActivity:", "我是关闭事件");
                backgroundAlpha(activity,1f);*/

            }
        });

        lineAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dissMissBack != null) {
                    dissMissBack.callBackFunction();
                }
                popupWindow.dismiss();
            }
        });


        // 设置好参数之后再show
        popupWindow.showAsDropDown(view);
    }


    public void backgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);
    }


    public void dismiss() {
        popupWindow.dismiss();
    }


}
